#include "state_2drpg.h"

void CState2DRPG::Init()
{

  m_Tag = "State2DRPG";
  
  m_pPlayer = NULL;
  
  m_bStarted = false;
  
  m_pGameCamera = new CGameCamera;

  
}

void CState2DRPG::Start()
{
	
	if( m_bStarted )
		return;
	
	m_pGameInterface->RegisterLuaEngineHooks();

	m_pGameInterface->OpenScript( LUA_2DRPG_PRERUN_FILE );

	m_pGameInterface->SetGameCamera( m_pGameCamera );
	
	m_pEntStateVec = m_pGameInterface->GetEntityFactory()->GetStateVec();
	m_pGameRules = m_pGameInterface->GetGameRules();
	m_pBattleSystem = m_pGameInterface->BattleSystem();
	m_pCollisionSystem = m_pGameInterface->CollisionSystem();
	
	Lua::SetGlobalInt( "State2DRPG", m_ID );
	
	font = m_pGameInterface->Graphics()->GetFont( "Uni14Bold" );
	
	/*
	m_pPlayer = m_pGameInterface->GetPlayer();
	m_pPlayer->Scale( 1.7, 1.7 );
	m_pPlayer->SetPos( 10, 10 );
	m_pPlayer->FitCollisionBoxToSprite( 0, 0.3f, .2f );
	m_pPlayer->ShiftCollisionBoxToBottom();
	
	m_pGameInterface->CallEngineHook( "LoadCurLevel" );
	
	CMap * map = m_pGameInterface->GetMap();
	
	m_pGameCamera->BindCameraToEntity( m_pPlayer );
	m_pGameCamera->SetBottomRightBounds( map->GetMapPixelWidth(), map->GetMapPixelHeight() );
	
	CEntity * e = new CEntity();
	
	e->AddSprite( m_pGameInterface->Graphics()->Find( "Graphics/Sprites/Battle/snoit.png" ), true );
	e->SetPos( 450, 750 );
	e->Scale( 2.0, 2.0 );
	e->FitCollisionBoxToSprite( 0, 1.0, 0.2 );
	e->SetAnimating( true );
	e->ShiftCollisionBoxToBottom();
	e->Flip();
	m_pGameInterface->GetEntityFactory()->GetStateVec()->AddEntity( e );
	
	e->SetHasShadow( true );
	e->SetShadowOffset( 10, -18 );
	/*
	e = new CEntity();
	
	e->AddSprite( m_pGameInterface->Graphics()->Find( "Graphics/Sprites/NPCs/huey.png" ), true );
	e->SetPos( 750, 850 );
	e->Scale( 1.7, 1.7 );
	e->FitCollisionBoxToSprite( 0, 1.0, 0.2 );
	e->SetAnimating( true );
	e->ShiftCollisionBoxToBottom();
	//e->Flip();
	m_pGameInterface->GetEntityFactory()->GetStateVec()->AddEntity( e );
	
	e->SetHasShadow( true );
	e->SetShadowOffset( 10, -18 );
	*/
	
	m_pGameInterface->CallEngineHook( "LoadCurLevel" );
	
	m_bStarted = true;
  
}

void CState2DRPG::PreRun()
{
	
	 m_iStateMessage = 1;

}

void CState2DRPG::Input()
{
	
	SDL_Event ev;
	
	SDL_PollEvent( &ev );
	
	Uint8 * keys = SDL_GetKeyState( NULL );

	bool moving = false;
	
	if( m_pPlayer && m_pGameRules->GetGameRule( "CanMove" ) == 1 )
	{
		
		bool moveleft = false, moveright = false;
		double vx = 0.0, vy = 0.0;
		
		if( keys[SDLK_a] )
			moveleft = true;
		else if( keys[SDLK_d] )
			moveright = true;
		
		if( keys[SDLK_w] )
		{
			
			moving = true;
			vy = -160.0;
			
			if( !moveright && !moveleft )
				m_pPlayer->SetCurrentSpriteBind( SB_UP );
			
		} else if( keys[SDLK_s] )
		{
			
			moving = true;
			vy = 160.0;
			
			if( !moveright && !moveleft )
				m_pPlayer->SetCurrentSpriteBind( SB_DOWNRUN );
			
		} 
		
		if( moveleft )
		{
			
			moving = true;
			
			vx = -160.0;
			m_pPlayer->SetCurrentSpriteBind( SB_LEFTRUN );

			
		} else if( moveright )
		{
			
			moving = true;
			
			vx = 160.0;
			m_pPlayer->SetCurrentSpriteBind( SB_RIGHTRUN );
			
		} 
		
		if( moving )
		{
			
			m_pPlayer->SetPhysicsVel( vx, vy );
			
		}

	}

	if( m_pPlayer )
	{
		
		if( !moving && m_pPlayer->VelocityIsZero() )
		{

			switch( m_pPlayer->GetCurrentSpriteBind() )
			{
				
				case SB_DOWNRUN:
					m_pPlayer->SetCurrentSpriteBind( SB_DOWN );
					break;
				case SB_LEFTRUN:
					m_pPlayer->SetCurrentSpriteBind( SB_LEFT );
					break;
				case SB_RIGHTRUN:
					m_pPlayer->SetCurrentSpriteBind( SB_RIGHT );
					break;
				case SB_UP:
					break;
				default:
					break;
					
			}
			
		} 
		
	}
	
			
	if( m_pPlayer && !moving )
		m_pPlayer->SetPhysicsVel( 0.0f, 0.0f );
		
	if( ev.key.keysym.sym == SDLK_ESCAPE )
	{
	
		m_iStateMessage = 0;
		
	}
	
	if( ev.type == SDL_QUIT )
	{
	
		m_iStateMessage = 0;
		
	}

}

void CState2DRPG::Think()
{
	
	if( ( !m_pPlayer && m_pGameInterface->PlayerCreated() ) || m_pGameInterface->ChangeMap() )
		m_pPlayer = m_pGameInterface->GetPlayer();
	
	m_pEntStateVec->EntitiesThink();
	
	m_pGameInterface->Think();
	
	m_pCollisionSystem->HandleEntityCollision();
	m_pCollisionSystem->HandleMapCollision();
	
	m_pGameInterface->CallLevelHook( "Think" );
	m_pGameInterface->CallEngineHook( "Think" );
	
	if( m_pBattleSystem->BattleStarting() ) 
		m_iStateMessage = STM_GOTO_RPGTRANS;
	

	
	
}

void CState2DRPG::Draw()
{

	CLEAR_BUFFER;
	
	PUSH_MATRIX;
	
		m_pGameCamera->Translate( m_pGameInterface->GetDPS() );
	
		m_pGameInterface->CallLevelHook( "PreMapDraw" );
	
		Draw::Color4f( 1.0f, 1.0f, 1.0f, 1.0f );
		
		//m_pGameInterface->GetMap()->DrawOutline();
		m_pGameInterface->GetMap()->DrawFrameBufferObjects( m_pGameCamera->GetCurTranslation() );
	
		//font->Draw( "My name is Sorin, prepare to die!", 100.0, 170.0 );
	
		m_pEntStateVec->DrawEntitiesToFrameBuffers();
		m_pGameInterface->GetFrameScene()->Draw();
	
	POP_MATRIX;

	m_pGameInterface->CallEngineHook( "PostDraw" );
	m_pGameInterface->CallLevelHook( "PostDraw" );
	
	SWAP_BUFFERS;
	
}